//
//  CategorViewController.m
//  Coco_Music
//
//  Created by K。 on 15/6/20.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "CategorViewController.h"

@interface CategorViewController ()


@end

@implementation CategorViewController
-(void)dealloc
{
    [_remenArray release];
    [_remenTitleArray release];
    [_wusunArray release];
    [_wusunTitleArray release];
    [_changjingArray release];
    [_changjingTitleArray release];
    [_yuyanArray release];
    [_yuyanTitleArray release];
    [_xinqingArray release];
    [_xinqingTitleArray release];
    [_fenggeArray release];
    [_fenggeTitleArray release];
    [_teseTitleArray release];
    [_niandaiArray release];
    [_niandaiTitleArray release];
    [_smArray release];
    [_parentnameArray release];
    [_songlist_nameArray release];
    [_small_pic_urlArray release];
    [_bigArray release];
    [_labelHeader1 release];
    [_singerVC release];
    [_mvVC release];
    [_cateVC release];
    [_collectionView release];
    [super dealloc];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UISegmentedControl *segmentV = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"分类",@"歌手",@"MV",nil]];
    segmentV.tintColor = [UIColor colorWithRed:244.0f/255.0f green:147.0f/255.0f blue:23.0f/255.0f alpha:1.0f];
    self.smArray =[NSMutableArray array];
    self.navigationItem.titleView = segmentV;
    [segmentV release];
    segmentV.selectedSegmentIndex = 0;
     [segmentV addTarget:self action:@selector(segmentCAction:) forControlEvents: UIControlEventValueChanged];
    self.singerVC = [[SingerViewController alloc]init];
    self.mvVC = [[MvViewController alloc]init];
    self.cateVC = [[CategorViewController alloc]init];
    [self addChildViewController:self.singerVC];
    [self addChildViewController:self.mvVC];
    [self.view addSubview:self.singerVC.view];
    [self.view addSubview:self.mvVC.view];
   //***
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 20 - 40) / 3, (([UIScreen mainScreen].bounds.size.width - 20 - 40) / 3+30) );
    flowLayout.minimumInteritemSpacing =  20;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.headerReferenceSize =CGSizeMake(375, 50);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -69-30) collectionViewLayout:flowLayout];
    self.collectionView .backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.collectionView ];
    [ self.collectionView release];
    self.collectionView .delegate = self;
    self.collectionView .dataSource = self;
       //注册
   [self.collectionView  registerClass:[CategorCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //在注册 头和底部
    [self.collectionView  registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    self.remenArray = [NSMutableArray array];
    self.remenTitleArray=[NSMutableArray array];
    self.wusunArray = [NSMutableArray array];
    self.wusunTitleArray=[NSMutableArray array];
    self.changjingArray = [NSMutableArray array];
    self.changjingTitleArray=[NSMutableArray array];
    self.yuyanArray = [NSMutableArray array];
    self.yuyanTitleArray=[NSMutableArray array];
    self.xinqingArray = [NSMutableArray array];
    self.xinqingTitleArray=[NSMutableArray array];
    self.fenggeArray = [NSMutableArray array];
    self.fenggeTitleArray=[NSMutableArray array];
    self.teseArray = [NSMutableArray array];
    self.teseTitleArray=[NSMutableArray array];
    self.niandaiArray= [NSMutableArray array];
    self.niandaiTitleArray=[NSMutableArray array];
    self.labelHeader1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 50, 20)] ;
      self.HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [self getData];


}
-(void)segmentCAction:(UISegmentedControl *)segment
{
 
    NSLog(@"index = %ld",(long)segment.selectedSegmentIndex);
    if (segment.selectedSegmentIndex == 1) {
        
        [self.view bringSubviewToFront:self.singerVC.view];
    }if (segment.selectedSegmentIndex == 2) {
        
        [self.view bringSubviewToFront:self.mvVC.view];
    }
     if (segment.selectedSegmentIndex == 0){
        
      
       [self.view bringSubviewToFront:self.collectionView];
         
        
    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return _remenArray.count;
    }
    
    if (section == 1) {
        return _wusunArray.count;
    }
    
    if (section == 2) {
        return _changjingArray.count;
    }
    
    if (section == 3) {
        return _yuyanArray.count;
    }
    
    if (section == 4) {
        return _xinqingArray.count;
    }
    
    if (section == 5) {
        return _fenggeArray.count;
    }
    
    if (section == 6) {
        return _teseArray.count;
    }
    
    if (section == 7) {
        return _niandaiArray.count;
    }else{
        
        return 0;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 8;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse"  forIndexPath:indexPath];
    if (indexPath.section == 0) {
        
        cell.picture = [_remenArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_remenTitleArray objectAtIndex:indexPath.row];
    }
    if (indexPath.section == 1) {
        cell.picture = [_wusunArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_wusunTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 2) {
        cell.picture = [_changjingArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_changjingTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 3) {
        cell.picture = [_yuyanArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_yuyanTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 4) {
        cell.picture = [_xinqingArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_xinqingTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 5) {
        cell.picture = [_fenggeArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_fenggeTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 6) {
        cell.picture = [_teseArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_teseTitleArray objectAtIndex:indexPath.row];
        
    }
    if (indexPath.section == 7) {
        cell.picture = [_niandaiArray objectAtIndex:indexPath.row];
        cell.titleSmall = [_niandaiTitleArray objectAtIndex:indexPath.row];
        
    }
    

   
    return  cell;
}



//------------------设置section文字---------
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
    
    
    
    for (UIView  * aaa in headerView.subviews) {
        [aaa removeFromSuperview];
    }
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        
        UILabel *aLabel = [[UILabel alloc] init];
        aLabel.frame = CGRectMake(5, 20, self.view.frame.size.width - 305, 20);
        [headerView addSubview:aLabel];
        aLabel.text = @"";
        aLabel.shadowColor = [UIColor colorWithRed:244.0f/255.0f green:147.0f/255.0f blue:23.0f/255.0f alpha:1.0f];
        
        headerView.backgroundColor = [UIColor whiteColor];
        if (indexPath.section == 0 && self.bigArray > 0) {
            
            aLabel.text =@"热门";
            [aLabel sizeToFit];
            
            
        }
        if (indexPath.section == 1&& self.bigArray > 0) {
            aLabel.text =@"无损";
            [aLabel sizeToFit];
            
        }
        if (indexPath.section == 2&& self.bigArray > 0) {
            aLabel.text =@"场景";
            [aLabel sizeToFit];
            
        }
        
        if (indexPath.section == 3&& self.bigArray > 0) {
            aLabel.text =@"语言";
            [aLabel sizeToFit];
            
        }
        if (indexPath.section == 4&& self.bigArray > 0) {
            aLabel.text =@"心情";
            [aLabel sizeToFit];
            
        }
        if (indexPath.section == 5&& self.bigArray > 0) {
            aLabel.text =@"风格";
            [aLabel sizeToFit];
        }
        if (indexPath.section == 6&& self.bigArray > 0) {
            aLabel.text =@"特色";
            [aLabel sizeToFit];
        }
        if (indexPath.section == 7&& self.bigArray > 0) {
            aLabel.text =@"年代";
            [aLabel sizeToFit];
        }
        
        return headerView;
        
    }
    return 0;
}

//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//
//    
//    if (kind == UICollectionElementKindSectionHeader) {
//        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
//        
//        headerView.backgroundColor = [UIColor whiteColor];
//        if (indexPath.section == 0) {
//            self.labelHeader1.text =@"热门";
//            [self.labelHeader1 sizeToFit];
//            [headerView addSubview:self.labelHeader1];
//            NSLog(@"111111");
//            return headerView;
//        }
//        if (indexPath.section == 1) {
//            self.labelHeader1.text =@"无损";
//            [self.labelHeader1 sizeToFit];
//            [headerView addSubview:self.labelHeader1];
//            NSLog(@"222222");
//            return headerView;
//        }
//        if (indexPath.section == 2) {
//            self.labelHeader1.text =@"场景";
//            [self.labelHeader1 sizeToFit];
//            [headerView addSubview:self.labelHeader1];
//            NSLog(@"33333");
//            return headerView;
//        }
//        
//      
//         if (indexPath.section == 3) {
//             self.labelHeader1.text =@"语言";
//             [self.labelHeader1 sizeToFit];
//             [headerView addSubview:self.labelHeader1];
//           return headerView;
//         }
//         if (indexPath.section == 4) {
//             self.labelHeader1.text =@"心情";
//             [self.labelHeader1 sizeToFit];
//             [headerView addSubview:self.labelHeader1];
//             return headerView;
//
//         }
//         if (indexPath.section == 5) {
//             self.labelHeader1.text =@"风格";
//             [self.labelHeader1 sizeToFit];
//             [headerView addSubview:self.labelHeader1];
//             return headerView;
//         }
//         if (indexPath.section == 6) {
//             self.labelHeader1.text =@"特色";
//             [self.labelHeader1 sizeToFit];
//             [headerView addSubview:self.labelHeader1];
//             return headerView;
//         }
//         if (indexPath.section == 7) {
//             self.labelHeader1.text =@"年代";
//             [self.labelHeader1 sizeToFit];
//             [headerView addSubview:self.labelHeader1];
//             return headerView;         }
//        
//        
//        
//        
//    }
//    return 0;
//}
//


-(void)getData
{
  [AFN_block NSString:@"http://fm.api.ttpod.com/channellist?image_type=240_200&app=ttpod&v=v7.9.4.2015052918" Blokc:^(id request) {
      NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:request];
      self.bigArray = [NSMutableArray array];
      self.bigArray = [dic objectForKeyedSubscript:@"data"];
      self.parentnameArray = [NSMutableArray array];
      self.small_pic_urlArray = [NSMutableArray array];
      self.songlist_nameArray = [NSMutableArray array];
      
      
      for (NSMutableDictionary *bigDic in self.bigArray) {
          Categor *storgeModel = [[Categor alloc] init] ;
          [storgeModel setValuesForKeysWithDictionary:bigDic];
          [self.smArray addObject:storgeModel];
          
          [self.parentnameArray addObject:storgeModel.parentname];
          [self.songlist_nameArray addObject:storgeModel.songlist_name];
          [self.small_pic_urlArray  addObject:storgeModel.small_pic_url];
      }
      
      [self setData];
      [self.HUD hide:YES];
      [self.collectionView reloadData];
  }];
  
}
-(void)setData
{
    
    for (int i = 0; i < _parentnameArray.count; i++) {
        if ([[self.parentnameArray objectAtIndex:i] isEqualToString:@"热门"]) {
            [self.remenArray addObject:[self.small_pic_urlArray objectAtIndex:i]];
            [self.remenTitleArray addObject:[self.songlist_nameArray objectAtIndex:i]];
        }
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"无损"]) {
            [_wusunArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [self.wusunTitleArray addObject:[self.songlist_nameArray objectAtIndex:i]];
        }
        
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"场景"]) {
            [_changjingArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_changjingTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
        }
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"语言"]) {
            [_yuyanArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_yuyanTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
        }
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"心情"]) {
            [_xinqingArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_xinqingTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
        }
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"风格"]) {
            [_fenggeArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_fenggeTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
        }
        if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"特色"]) {
            [_teseArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_teseTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
            
        }if ([[_parentnameArray objectAtIndex:i] isEqualToString:@"年代"]) {
            [_niandaiArray addObject:[_small_pic_urlArray objectAtIndex:i]];
            [_niandaiTitleArray addObject:[_songlist_nameArray objectAtIndex:i]];
        }
        
        
    }
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategorDetailViewController *catDVC = [[CategorDetailViewController alloc]init];
   
    
     Categor *storgeModel =[self.smArray objectAtIndex:indexPath.row];
    catDVC.SongList_id =[storgeModel.songlist_id integerValue];

    
   
   [self.navigationController pushViewController:catDVC animated:YES];
   // NSLog(@"row = %ld section = %ld ",indexPath.row, indexPath.section);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
